<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Requests\VisitWebTask\Create;
use App\Http\Requests\VisitWebTask\Last;
use App\Http\Requests\VisitWebTask\Show;
use App\Models\Device;
use App\Models\VisitWebTask;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class VisitWebTaskController extends Controller
{
    protected $result = ['code' => 1, 'msg' => '', 'data' => []];
    const FINAL_CODE = 0;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show(Show $request)
    {
        $validated = $request->validated();

        $model = VisitWebTask::query()->where(['id' => $validated['id']])->firstOrFail();
        if ($model) {
            $model->file = env('FILE_PREFIX', 'http://118.212.37.42:18018/storage/') . $model->file;
            return ['code' => 200, 'message' => 'success', 'data' => $model];
        } else {
            return ['code' => 404, 'message' => 'fail', 'data' => []];
        }
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function last(Last $request)
    {
        $validated = $request->validated();

//        Log::info(print_r($request->post(),true));
        $model = VisitWebTask::query()->wherein('status', [VisitWebTask::STATUS_CREATED, VisitWebTask::STATUS_PULLED, VisitWebTask::STATUS_RETRY])->where(['device_id' => $request->device_id])->orderBy('id')->first();
        if ($model) {
            $this->result = ['code' => self::FINAL_CODE, 'msg' => '', 'data' => [clone $model]];
            $model->status = VisitWebTask::STATUS_PULLED;
            $model->save();
        }
        return $this->result;
    }

    public function done(Request $request)
    {
//        Log::debug($request);
        $path = $request->file('file')->store('public');
        $path = str_replace('public/', '', $path);
        $model = VisitWebTask::query()->find($request->task_id);
        $model->status = VisitWebTask::STATUS_DONE;
        $model->final_url = $request->final_url;
        $model->file = $path;
        $model->updated_at = Carbon::now();
        $model->save();
        $this->result = ['code' => self::FINAL_CODE, 'msg' => 'done', 'data' => []];
    }

    public function create(Create $request)
    {
        $validated = $request->validated();
        unset($validated['sign']);
        unset($validated['time']);
        unset($validated['sign']);
//        unset($validated['agent_code']);
        $device = $this->scheduleDevice();
        $validated['device_id'] = $device->device_id;
        $model = VisitWebTask::query()->create($validated);
        if ($model) {
            return ['code' => 200, 'message' => '创建成功', 'data' => $model];
        } else {
            return ['code' => 404, 'message' => '创建失败', 'data' => []];
        }
    }

    /**
     * @return Device|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object
     */
    private function scheduleDevice()
    {
        //todo 规律
        return Device::query()->where('heart_at','>',Carbon::now()->subHours(1))->orderBy('location_at')->firstOrFail();
    }
}
